package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.darkforest.auth.CurrentMember;
import org.darkforest.dao.mapper.MemberRemindSettingMapper;
import org.darkforest.dao.mapper.MessageMapper;
import org.darkforest.model.Member;
import org.darkforest.model.MemberRemindSetting;
import org.darkforest.model.Message;
import org.darkforest.model.RestEntity;
import org.darkforest.auth.AuthPassport;
import org.darkforest.ucenter.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/2.
 */
@RestController
@RequestMapping(value = "msg")
@Api(value = "msg", description = "消息",produces= MediaType.APPLICATION_JSON_VALUE)
public class MessageController {
    @Autowired
    private MemberRemindSettingMapper memberRemindSettingMapper;
    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private MessageService messageService;

    @AuthPassport
    @RequestMapping(value = "myMessage",method = RequestMethod.GET)
    @ApiOperation(value = "消息列表", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity myMessage(@CurrentMember Member member,
                                @RequestParam(required = false, defaultValue = "1") int p,
                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        try{
            MemberRemindSetting memberRemindSetting = memberRemindSettingMapper.selectByPrimaryKey(member.getMemberId());
            List<Message> displayMessageList = new ArrayList<Message>();
            if(memberRemindSetting==null){
                memberRemindSetting = new MemberRemindSetting();
                memberRemindSetting.setrContactsRemindSwtich("1");
                memberRemindSetting.setrOfferRemindSwtich("1");
                memberRemindSetting.setrSystemMsgSwitch("1");
            }

            if("1".equals(memberRemindSetting.getrSystemMsgSwitch())){
                List<Message> sysList = messageService.selectByCondition(new Message(member.getMemberId()+"",0,p,pageSize));
                displayMessageList.addAll(sysList);
            }
            if ("1".equals(memberRemindSetting.getrOfferRemindSwtich())) {
                List<Message> offerList = messageService.selectByCondition(new Message(member.getMemberId()+"",1,p,pageSize));
                displayMessageList.addAll(offerList);
            }
            if ("1".equals(memberRemindSetting.getrContactsRemindSwtich())) {
                List<Message> contactList = messageService.selectByCondition(new Message(member.getMemberId()+"",2,p,pageSize));
                displayMessageList.addAll(contactList);
            }
            return RestEntity.success(displayMessageList);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    /**
     * 检查是否有未读消息
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "checkUnReadMsg",method = RequestMethod.GET)
    @ApiOperation(value = "检查是否有未读消息:01 有、02 无", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity checkUnReadMsg(@CurrentMember Member member){
        try{
            MemberRemindSetting memberRemindSetting = memberRemindSettingMapper.selectByPrimaryKey(member.getMemberId());
            if(memberRemindSetting==null){
                memberRemindSetting = new MemberRemindSetting();
                memberRemindSetting.setrContactsRemindSwtich("1");
                memberRemindSetting.setrOfferRemindSwtich("1");
                memberRemindSetting.setrSystemMsgSwitch("1");
            }

            List<Integer> list = new ArrayList<Integer>();

            if("1".equals(memberRemindSetting.getrSystemMsgSwitch())){
                list.add(0);
            }

            if ("1".equals(memberRemindSetting.getrOfferRemindSwtich())) {
                list.add(1);
            }

            if ("1".equals(memberRemindSetting.getrContactsRemindSwtich())) {
                list.add(2);
            }
            Integer count = messageService.getNotReadCount(member.getMemberId(),list);
            if(count>0){
                return RestEntity.success("01");
            }else{
                return RestEntity.success("02");
            }
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }


    @AuthPassport
    @RequestMapping(value = "delMsg",method = RequestMethod.GET)
    @ApiOperation(value = "删除消息", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity delMsg( @RequestParam(required = true) Integer[] mId,@CurrentMember Member member){
        try{
            messageService.delMessageById(mId, member.getMemberId());
            return RestEntity.success("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }


    @AuthPassport
    @RequestMapping(value = "msgDetail",method = RequestMethod.GET)
    @ApiOperation(value = "获取消息详情", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity msgDetail( @RequestParam(required = true) Integer mid){
        try{
            messageMapper.updateByPrimaryKeySelective(new Message(mid,1));
            Message message = messageMapper.selectByPrimaryKey(mid);
            return RestEntity.success(message);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }
}
